package cn.idongjia.logloader;

import cn.idongjia.logloader.mongo.LogInfo;
import cn.idongjia.logloader.mongo.LogLevel;
import cn.idongjia.logloader.mongo.MongoJobLogger;
import org.apache.commons.cli.*;
import org.apache.commons.cli.ParseException;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.lang.reflect.Field;
import java.text.*;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import java.util.SimpleTimeZone;

/**
 * Created by renxianlong on 16/4/29.
 */
public class LogViewCommand {

    public static String ROOT_PATH = System.getProperty("logview.home");

    public static String CONF_FILE = ROOT_PATH + "/conf/logview.properties";

    public static MongoJobLogger jobLogger;

    public static SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmssSSS");

    public static void main(String[] args) {

        CommandLine commandLine =
                parseCmdLine("logview", args, buildCommandlineOptions(new Options()),
                        new PosixParser());
        if (null == commandLine) {
            System.exit(-1);
            return;
        }

        Properties props = new Properties();

        try {
            File file = new File(CONF_FILE);
            props.load(new FileInputStream(file));
        } catch (IOException e) {
            e.printStackTrace();
        }

        jobLogger = new MongoJobLogger(props);

        System.out.println("---------------------------------------------------");

        Param param = buildLogInfo(commandLine);

        jobLogger.query(param);
    }

    private static Param buildLogInfo(CommandLine commandLine) {
        Param param = new Param();
        if (commandLine.hasOption("limit")){
            param.setLimit(Integer.parseInt(commandLine.getOptionValue("limit")));
        }
        if (commandLine.hasOption("level")){
            param.setLogLevel(commandLine.getOptionValue("level"));
        }
        if (commandLine.hasOption("mid")){
            param.setModuleId(commandLine.getOptionValue("mid"));
        }
        if (commandLine.hasOption("sid")){
            param.setServerId(commandLine.getOptionValue("sid"));
        }
        if (commandLine.hasOption("mid")){
            param.setModuleId(commandLine.getOptionValue("mid"));
        }
        if (commandLine.hasOption("logname")){
            param.setLogName(commandLine.getOptionValue("logname"));
        }
        if (commandLine.hasOption("msg")){
            param.setMsg(commandLine.getOptionValue("msg"));
        }
        if (commandLine.hasOption("bt")){
            String beginTm = commandLine.getOptionValue("bt");
            try {
                param.setBt(format.parse(beginTm).getTime());
            } catch (java.text.ParseException e) {
                e.printStackTrace();
            }
        }
        if (commandLine.hasOption("et")){
            String beginTm = commandLine.getOptionValue("et");
            try {
                param.setEt(format.parse(beginTm).getTime());
            } catch (java.text.ParseException e) {
                e.printStackTrace();
            }
        }
        if (commandLine.hasOption("ga")){
            param.setMsg(commandLine.getOptionValue("ga"));
        }
        if (commandLine.hasOption("gb")){
            param.setMsg(commandLine.getOptionValue("gb"));
        }
        if (commandLine.hasOption("gc")){
            param.setMsg(commandLine.getOptionValue("gc"));
        }

        return param;
    }

    public void grep(){

    }

    public static Options buildCommandlineOptions(final Options options) {
        Option opt = new Option("h", "help", false, "Print help");
        opt.setRequired(false);
        options.addOption(opt);

        opt = new Option("t", "tailf", false, "tailf");
        opt.setRequired(false);
        options.addOption(opt);

        opt = new Option("limit", "limit", true, "limit");
        opt.setRequired(false);
        options.addOption(opt);

        opt = new Option("level", "logLevel", true, "logLevel, eg: debug,info,warn,error");
        opt.setRequired(false);
        options.addOption(opt);

        opt = new Option("mid", "moduleId", true, "moduleId, eg: tianji,web");
        opt.setRequired(false);
        options.addOption(opt);

        opt = new Option("sid", "serverId", true, "服务器ip或者别名, eg: localhost,tianji1,web1");
        opt.setRequired(false);
        options.addOption(opt);

        opt = new Option("logname", "logName", true, "日志名, eg: 类名");
        opt.setRequired(false);
        options.addOption(opt);

        opt = new Option("msg", "message", true, "grep,此处填消息内容");
        opt.setRequired(false);
        options.addOption(opt);

        opt = new Option("bt", "begintm", true, "起始时间 格式为 20150503170324567 格式切记不能错");
        opt.setRequired(false);
        options.addOption(opt);

        opt = new Option("et", "endtm", true, "截止时间 格式为 20150503170324567 格式切记不能错");
        opt.setRequired(false);
        options.addOption(opt);

        opt = new Option("gc", "grep -C", true, "统计匹配的有几行");
        opt.setRequired(false);
        options.addOption(opt);

        opt = new Option("ga", "grep -A", true, "显示匹配行和之后的几行,具体行数由limit参数来约束");
        opt.setRequired(false);
        options.addOption(opt);

        opt = new Option("gb", "grep -A", true, "显示匹配行和之前的几行,具体行数由limit参数来约束");
        opt.setRequired(false);
        options.addOption(opt);
        return options;
    }

    public static CommandLine parseCmdLine(final String appName, String[] args, Options options,
                                           CommandLineParser parser) {
        HelpFormatter hf = new HelpFormatter();
        hf.setWidth(110);
        CommandLine commandLine = null;
        try {
            commandLine = parser.parse(options, args);
            if (commandLine.hasOption('h')) {
                hf.printHelp(appName, options, true);
                return null;
            }
        }
        catch (ParseException e) {
            hf.printHelp(appName, options, true);
        }

        return commandLine;
    }
}
